<template>
  <a-select v-model="warehouseId" class="wh-sel" :options="list"></a-select>
</template>

<script lang="ts" setup>
  import { ref, watch } from 'vue';
  import type { SelectOptionData } from '@arco-design/web-vue/es/select/interface';
  import { getAllWH } from '@/api/warehouse/list';

  const props = defineProps({
    modelValue: {
      type: Number,
      default: undefined
    },
    all: {
      type: Boolean,
      default: false
    }
  });
  const warehouseId = ref(props.modelValue);

  const emit = defineEmits(['update:modelValue']);
  watch(warehouseId, newv => {
    emit('update:modelValue', newv);
  });
  watch(props, newp=>{
    console.log(100, newp);
    warehouseId.value = newp.modelValue;
  });
  const list = ref([] as SelectOptionData[]);
  async function loadData() {
    if(props.all) {
      list.value = await getAllWH();
      return;
    }

    let ul = JSON.parse(localStorage['user.login'] || '{}');
    if(ul && ul.warehouseId) {
      list.value = [{
        label: ul.warehouseName,
        value: ul.warehouseId
      }];
      warehouseId.value = ul.warehouseId;
      // emit('update:modelValue', ul.warehouseId);
    }
  }
  loadData();

</script>
